﻿using System;
using System.Collections.Generic;
using System.Web;

namespace UserInterface.Misc
{
    public static class SessionManager
    {
        public const string SessionKey = "UserSession";
        private static IList<Session> activeSessions;

        static SessionManager()
        {
            activeSessions = new List<Session>();
        }

        public static Session NewSession(int userId)
        {
            Session session;

            // TODO: kan niet zomaar een userid veranderen??
            if (CurrentSession != null)
            {
                session = CurrentSession;
                session.UserId = userId;
            }
            else
            {
                session = new Session(userId, DateTime.Now);
                HttpContext.Current.Session[SessionKey] = session;
                activeSessions.Add(session);
            }

            return session;
        }

        public static void Remove(Session session)
        {
            activeSessions.Remove(session);
        }

        public static Session CurrentSession
        {
            get { return HttpContext.Current.Session[SessionKey] as Session; }
        }

        public static IList<Session> ActiveSessions
        {
            get { return activeSessions; }
        }
    }
}
